package data.news;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArticleIterator implements Iterator<Article>{

	public static Pattern ID_PATTERN = Pattern.compile("<article docID = \"(\\d+)\">");
	public static long getArticleID(String line){
		Matcher matcher = ID_PATTERN.matcher(line);
		matcher.matches();
		return Long.parseLong(matcher.group(GROUP_IDX));
	}
	private static int GROUP_IDX = 1;
	private Article nextArticle = null;
	private String currentLine = null;
	private BufferedReader reader = null;
	public ArticleIterator(BufferedReader reader) {
		this.reader = reader;
	}
	public boolean hasNext() {
		if (nextArticle != null) {
			return true;
		}
		try {
			while ((currentLine = reader.readLine())!=null){
				if (currentLine.contains("<article")) {
					Matcher matcher = ID_PATTERN.matcher(currentLine);
					if (!matcher.matches()) {
							System.out.println("no id pattern in:"+currentLine);
					}
					else{
						nextArticle = new Article(Long.parseLong(matcher.group(GROUP_IDX)));
						return true;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Article next() {
		if (nextArticle == null) {
			hasNext();
		}
		try {
			Article result = nextArticle;
			nextArticle = null;
			boolean startContent = false;
			StringBuffer contentBuffer = new StringBuffer();
			while ((currentLine = reader.readLine())!=null){
				if (currentLine.contains("</article>")) {
					contentBuffer.deleteCharAt(contentBuffer.length()-1);
					result.setContent(contentBuffer.toString());
					return result;
				}
				if (currentLine.contains("</contents>")){
					startContent = false;
					continue;
				}
				else if (currentLine.contains("<contents>")) {
					startContent = true;
					continue;
				}

				if (startContent){
					contentBuffer.append(currentLine);
					contentBuffer.append("\n");
				}
				else if (currentLine.contains("<url")) {
					result.setUrl(getValue(currentLine));
				}
				else if (currentLine.contains("<sourcename")) {
					result.setSourcename(getValue(currentLine));
				}
				else if (currentLine.contains("<author")) {
					result.setAuthor(getValue(currentLine));
				}
				else if (currentLine.contains("<title")) {
					if (!currentLine.contains("</title>")) {
						String line = null;
						while((line = reader.readLine())!=null){
							currentLine+=line;
							if (currentLine.contains("</title>")) {
								break;
							}
						}
					}
					result.setTitle(getValue(currentLine));
				}
				else if (currentLine.contains("<date")) {
					result.setDate(Integer.parseInt(getValue(currentLine)));
				}
				else if (currentLine.contains("<predictedArticleType")) {
					result.setPredictedarticletype(getValue(currentLine));
				}
				else if (currentLine.contains("<articleType")) {
					result.setArticletype(getValue(currentLine));
				}
				else if (currentLine.contains("<syndicate")) {
					result.setSyndicate(getValue(currentLine));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	private String getValue(String node){
		node = node.substring(node.indexOf(">")+1);
		node = node.substring(0, node.lastIndexOf("<"));
		return node;
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}
}
